package com.gas.project.gas.message.controller;

import java.util.Date;
import java.util.List;

import com.gas.common.utils.poi.ExcelUtil;
import com.gas.framework.aspectj.lang.annotation.Log;
import com.gas.framework.aspectj.lang.enums.BusinessType;
import com.gas.framework.web.controller.BaseController;
import com.gas.framework.web.domain.AjaxResult;
import com.gas.framework.web.page.TableDataInfo;
import com.gas.project.system.user.domain.User;
import com.gas.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gas.project.gas.message.domain.Message;
import com.gas.project.gas.message.service.IMessageService;

/**
 * 消息沟通Controller
 *
 * @author xyl
 * @date 2022-04-05
 */
@Controller
@RequestMapping("/gas/message")
public class MessageController extends BaseController {
    private String prefix = "gas/message";

    @Autowired
    private IMessageService messageService;
    @Autowired
    private IUserService userService;

    @GetMapping()
    public String message(String moduleName,String courseNumber,String receiveNumber,ModelMap mmap) {
        mmap.put("user",getSysUser());
        mmap.put("moduleName",moduleName);
        mmap.put("courseNumber",courseNumber);
        mmap.put("receiveNumber",receiveNumber);

        User sysUser = getSysUser();
        Message message = new Message();
        message.setSubmitId(sysUser.getUserId());
        User user = userService.selectUserByLoginName(receiveNumber);
        message.setReceiveId(user.getUserId());
        message.setModuleName(moduleName);
        message.setCourseNumber(courseNumber);
        List<Message> messages = messageService.selectMessageList(message);
        mmap.put("messages",messages);
        return prefix + "/message";
    }

    /**
     * 查询消息沟通列表
     */
    @RequiresPermissions("gas:message:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Message message) {
        startPage();
        List<Message> list = messageService.selectMessageList(message);
        return getDataTable(list);
    }

    /**
     * 导出消息沟通列表
     */
    @RequiresPermissions("gas:message:export")
    @Log(title = "消息沟通", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Message message) {
        List<Message> list = messageService.selectMessageList(message);
        ExcelUtil<Message> util = new ExcelUtil<Message>(Message.class);
        return util.exportExcel(list, "message");
    }

    /**
     * 新增消息沟通
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存消息沟通
     */
    @RequiresPermissions("gas:message:add")
    @Log(title = "消息沟通", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Message message) {
        message.setSubmitId(getSysUser().getUserId());
        User user = userService.selectUserByLoginName(message.getReceiveNumber());
        message.setReceiveId(user.getUserId());
        message.setStatus(0);
        message.setSubmitTime(new Date());
        return toAjax(messageService.insertMessage(message));
    }

    /**
     * 修改消息沟通
     */
    @GetMapping("/edit/{messageId}")
    public String edit(@PathVariable("messageId") Integer messageId, ModelMap mmap) {
        Message message = messageService.selectMessageById(messageId);
        mmap.put("message", message);
        return prefix + "/edit";
    }

    /**
     * 修改保存消息沟通
     */
    @RequiresPermissions("gas:message:edit")
    @Log(title = "消息沟通", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Message message) {
        return toAjax(messageService.updateMessage(message));
    }

    /**
     * 删除消息沟通
     */
    @RequiresPermissions("gas:message:remove")
    @Log(title = "消息沟通", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(messageService.deleteMessageByIds(ids));
    }
}
